package com.ttt.tank;

import com.ttt.game.GameFrame;
import com.ttt.game.LevelInof;
import com.ttt.util.Constant;
import com.ttt.util.EnemyTanksPool;
import com.ttt.util.MyUtil;

import java.awt.*;

/**
 * 敌人坦克类1
 */
public class EnemyTank extends Tank{
    public static final int Type_light = 0;
    public static final int Type_haven = 1;
    public int type = Type_light;
    //敌人坦克的图片数组
    private static Image[] lightImg;
    private static Image[] havenImg;
    //记录5秒开始的时间
    private  long aiTime;
    //在静态代码块中对它初始化
    static {
        lightImg = new Image[4];
        lightImg[0] = MyUtil.createImage("images/Etankup.png");
        lightImg[1] = MyUtil.createImage("images/Etankdown.png");
        lightImg[2] = MyUtil.createImage("images/Etankleft.png");
        lightImg[3] = MyUtil.createImage("images/Etankright.png");
        havenImg = new Image[4];
        havenImg[0] = MyUtil.createImage("images/Etank2up.png");
        havenImg[1] = MyUtil.createImage("images/Etank2down.png");
        havenImg[2] = MyUtil.createImage("images/Etank2left.png");
        havenImg[3] = MyUtil.createImage("images/Etank2right.png");
    }

    private EnemyTank(int x, int y, int dir) {
        super(x, y, dir);
        //敌人一旦创建就计时
        aiTime = System.currentTimeMillis();
        type = MyUtil.getRandomNumber(0,2);
}

    public EnemyTank() {
        type = MyUtil.getRandomNumber(0,2);
        aiTime = System.currentTimeMillis();
    }

    //用于创建一个敌人的坦克
    public static Tank createEnemy(){
        int x = MyUtil.getRandomNumber(0,2) == 0 ? Radius :  Constant.Frame_Width - Radius;
        int y = GameFrame.titleBraH + Radius;
        int dir = Dir_Down;
//        Tank enemy = new EnemyTank(x,y,dir);
        EnemyTank enemy = (EnemyTank)EnemyTanksPool.get();
        enemy.setX(x);
        enemy.setY(y);
        enemy.setDir(dir);
        enemy.setEnemy(true);
        enemy.setState(State_Move);
        //根据游戏的难度设置敌人的血量
        int maxHP = Tank.Default_Hp * LevelInof.getInstance().getLevelType();
        enemy.setHp(maxHP);
        enemy.setMaxHP(maxHP);
        enemy.setHp(Tank.Default_Hp * LevelInof.getInstance().getLevelType());
        //通过关卡信息中的敌人类型，来设置当前出生的敌人的类型
        int enemyType = LevelInof.getInstance().getRandomEnemyType();
        enemy.setType(enemyType);

        return enemy;
    }

    @Override
    public void drawImgTank(Graphics g) {
        ai();
        switch (type) {
            case Type_light:
                g.drawImage(lightImg[getDir()], getX() - Radius, getY() - Radius, null);
                break;
            case Type_haven:
                g.drawImage(havenImg[getDir()], getX() - Radius, getY() - Radius, null);
        }
    }

    /**
     * 敌人AI
     */
    private void ai(){
        if (System.currentTimeMillis() - aiTime > Constant.Enemy_AI_Interval){
            //间隔2秒随机一个状态
            setDir(MyUtil.getRandomNumber(Dir_Up,Dir_Right+1));
            setState(MyUtil.getRandomNumber(0,2) == 0 ? State_Stand : State_Move);
            aiTime = System.currentTimeMillis();
        }
        if (Math.random() < Constant.Enemy_Fire_Percent){
            fire();
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }
}
